package com.source.boot.controller;

import com.source.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplicationRunListener;
import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ApplicationListener;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author wengqy
 * @Date 2021/1/22
 */
@RestController
public class HelloController {

        @Autowired
        HelloService helloService;

        @GetMapping("/starter")
        public String customer(){
            return helloService.sayHello("China");
        }


    public String customer22(){

        ApplicationContextInitializer applicationContextInitializer = null;
        ApplicationListener applicationListener =null;
        SpringApplicationRunListener applicationRunListener = null;
        ApplicationRunner applicationRunner =null;
        CommandLineRunner commandLineRunner = null;



        return null;
    }







//<h1>form</h1>
//<form action="/save" method="post">
//    name:  <input type="text" name="name"><br>
//    age: <input type="text" name="age"><br>
//      <input type="submit"value="提交"><br>
//
//</form>
    @PostMapping("/save")
    public Map<String,Object> save(@RequestBody  String content){
        Map<String,Object>   map  = new HashMap();
        map.put("content",content);
        return map;  // {"content":"name=meng&age=18"}
    }



// Cookie: Idea-35c85865=296d60a0-50ff-4448-b593-058b9ae546c6; Hm_lvt_080836300300be57b7f34f4b3e97d911=1591705213; _jspxcms=c900c90dad6b4ac2bcf54e4858051f7e

    // <a href="/requestParam?name=quan&inters=lanqiu&inters=zuqiu">requestParam</a>
    @GetMapping("/cookie")
    public Map<String,Object> cookie(@CookieValue("Idea-35c85865") String name,
                                     @CookieValue("Idea-35c85865") Cookie cookie
                                     ){
        Map<String,Object>   map  = new HashMap();
        map.put("Idea-35c85865",name);
        map.put("cookie",cookie);
        System.out.println(cookie);

        return map;
     //   {"Idea-35c85865":"296d60a0-50ff-4448-b593-058b9ae546c6",
        //   "cookie":{"name":"Idea-35c85865","value":"296d60a0-50ff-4448-b593-058b9ae546c6","version":0,
        //   "comment":null,"domain":null,"maxAge":-1,"path":null,"secure":false,"httpOnly":false}}
    }







  // http://localhost:8080/requestParam?name=weng&inters=basketball&inters=football
    @GetMapping("/requestParam")
    public Map<String,Object> requestParam(@RequestParam("name") String name,
                                           @RequestParam("inters") List<String> inters,
                                           @RequestParam Map<String,String> params){
        Map<String,Object>   map  = new HashMap();
        map.put("name",name);
        map.put("inters",inters);
        map.put("params",params); //获取全部参数
        return map;
        //  {"inters":["basketball","football"],"name":"weng","params":{"name":"weng","inters":"basketball"}}
        // {"inters":["basketball","football"],"name":"weng"}
    }










    // http://localhost:8080/path/aa/23
    @GetMapping("/path/{name}/{age}")
    public Map<String,Object> pathVariable(@PathVariable("name") String name,@PathVariable("age") String age,
                                           @PathVariable Map<String,String> mp,
                                           @RequestHeader("User-Agent") String UserAgent,
                                           @RequestHeader Map<String,String> headers ){
        Map<String,Object>   map  = new HashMap();
        map.put("name",name);
        map.put("age",age);
        map.put("mp",mp);
        map.put("UserAgent",UserAgent);
        map.put("headers",headers);
        return  map;

//           {"headers":{"host":"localhost:8080","connection":"keep-alive","cache-control":"max-age=0","upgrade-insecure-requests":"1",
//           "user-agent":"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko)
//         * Chrome/87.0.4280.66 Safari/537.36","accept":"text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,
//         * */*;q=0.8,application/signed-exchange;v=b3;q=0.9","sec-fetch-site":"none","sec-fetch-mode":"navigate","sec-fetch-user":"?1"," +
//            "    "sec-fetch-dest":"document","accept-encoding":"gzip, deflate, br","accept-language":"zh-CN,zh;q=0.9,en;q=0.8","
//         cookie":"Idea-35c85865=296d60a0-50ff-4448-b593-058b9ae546c6; Hm_lvt_080836300300be57b7f34f4b3e97d911=1591705213;
//         _jspxcms=c900c90dad6b4ac2bcf54e4858051f7e"},"mp":{"name":"aa","age":"23"},"name":"aa","UserAgent":"Mozilla/5.0
//          (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.66 Safari/537.36","age":"23"}

    }

}
